/*
    Pureport, a report generator for Java
    Copyright (C) 2011  Maurice Perry <maurice@perry.ch>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.librebiz.pureport.context;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.font.GraphicAttribute;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;

public class ImageAttribute extends GraphicAttribute implements Serializable {
    private Image image;
    private float originX;
    private float originY;
    private float width;
    private float height;

    public ImageAttribute(Image image, int alignment,
            float width, float height, float originX, float originY) {
        super(alignment);
        this.image = image;
        this.width = width <= 0 ? image.getWidth(null) : width;
        this.height = height <= 0 ? image.getHeight(null) : height;
        this.originX = originX;
        this.originY = originY;
    }

    public float getAscent() {
        return originY <= 0 ? 0 : originY;
    }

    public float getDescent() {
        return height <= originY ? 0 : height-originY;
    }

    public float getAdvance() {
        return width <= originX ? 0 : width-originX;
    }

    @Override
    public Rectangle2D getBounds() {
        return new Rectangle2D.Float(
                        -originX, -originY, width, height);
    }

    public void draw(Graphics2D g2d, float x, float y) {
        g2d.drawImage(image, (int) (x-originX), (int) (y-originY),
                (int)width, (int)height, null);
    }
}
